package com.atcc.demo.controller;

import java.util.List;
import javax.validation.constraints.NotEmpty;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atcc.demo.dao.Person;
import com.atcc.demo.dao.StudentInfo;
import com.atcc.demo.service.PersonService;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value="用户controller",tags= {"用户操作接口"})
@RestController
@RequestMapping("/Student")
public class PersonController {
	@Autowired
	private PersonService personService;
	
	@ApiOperation(value="根据id查询学生信息",notes="根据id查询学生信息")
	@GetMapping("/select/id")
	public Person selectStudent(
			@RequestParam(name="学院",required=false,defaultValue="信息学院") String collage,
			@RequestParam(name="student_token") String token,
			@NotEmpty(message="id不能为空")
			@RequestParam(name="学生id") int id) {
		Person resultstu = personService.selectByPersonId(id);
		System.out.println("查询结果:" + resultstu);
		return resultstu;
	}
	
	@ApiOperation(value="根据name查询学生信息",notes="根据name查询学生信息")
	@GetMapping("/select/name")
	public List<StudentInfo> selectStudent(
			@RequestParam(name="学院",required=false,defaultValue="信息学院") String college,
			@RequestParam(name="student_token") String token,
			@RequestParam(name="name") String name) {
		List<StudentInfo> resultstu = personService.selectByName(name);
		System.out.println("查询结果:" + resultstu);
		return resultstu;
	}
	
	
	@ApiOperation(value="查询总学生信息",notes="查询总学生信息")
	@GetMapping("/selectAll")
	public List<Person> selectAllStudent(
			@RequestParam(name="学院",required=false,defaultValue="信息学院") String collage) {
		List<Person> result = personService.selectAllPerson();
		System.out.println("查询结果: " + result);
		return result;
		
	}

	@ApiOperation(value="测试",notes="测试")
	@GetMapping("/test")
	public String test(){
		return "这是一个测试";
		
	}
	
	@ApiOperation(value="增添学生信息",notes="增添学生信息")
	@PostMapping("/add")
	public int addStudent(
			@RequestBody Person person) {
		    int result = personService.addPerson(person);
		    System.out.println("结果是：" + person);
		    return result;
	}
	
	@ApiOperation(value="根据id删除学生信息",notes="根据id删除学生信息")
	@DeleteMapping("/delete")
	public int deletestudent(@RequestParam(name="学生id") String id) {
		 int result = personService.deleteByPersonId(id);
		 System.out.println("删除了：" + id);
		 return result;
	}
	
	/*@RequestMapping(value="/select")
	public String students() {
		Person result = personService.selectByPersonId(2);
		System.out.println("插入的结果是:" + result);
		return result+"";
	}
	@RequestMapping(value="/selectall")
	public String studentsall() {
		List<Person> result = personService.selectAllPerson();
		System.out.println("插入的结果是:" + result);
		return result+"";
	}*/
}
